<?php
/**
 * Nguoi tao: 
 * Ngay tao: 
 * Y nghia: 
 */	
class mtl_searchController extends  Zend_Controller_Action {
	public function init(){
		Zend_Loader::loadClass('mtl_modSearch');
		Zend_Loader::loadClass('listxml_modTree');
		Zend_Loader::loadClass('Zend_Json');	    
		//Load cau hinh thu muc trong file config.ini
        $tempDirApp = Zend_Registry::get('conDirApp');
		$this->_dirApp = $tempDirApp->toArray();
		$this->view->dirApp = $tempDirApp->toArray();
		//Lay duong dan thu muc goc (path directory root)
		$this->view->baseUrl = $this->_request->getBaseUrl() . "/public/";
		if (!$this->_request->isXmlHttpRequest()){
			//Cau hinh cho Zend_layout
			Zend_Layout::startMvc(array(
				    'layoutPath' => $this->_dirApp['layout'],
				    'layout' => 'index'			    
				    ));	
			//Load cac thanh phan cau vao trang layout (index.phtml)
			$response = $this->getResponse();
			//Ky tu dac biet phan tach giua cac phan tu
			$this->view->delimitor 	= "!~~!";	
			//Load cau hinh thu muc trong file config.ini de lay ca hang so dung chung
	        $tempConstPublic = Zend_Registry::get('ConstPublic');
			$this->_ConstPublic = $tempConstPublic->toArray();	
			//Lay cac hang so su dung trong JS public
			$objConfig = new Efy_Init_Config();
			$objConst = new Efy_Init_Const();
			$this->view->JSPublicConst = $objConfig->_setJavaScriptPublicVariable();		
			$this->view->UrlAjax = $objConfig->_setUrlAjax();
			$arrConst = $objConst->_setProjectPublicConst();
			$this->view->arrConst = $arrConst;
			// Load tat ca cac file Js va Css
			$this->view->LoadAllFileJsCss = Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-style','mtl/search.css',',','css')
			.Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-js','Picbox/css/picbox.css',',','css')
			.Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-js','mtl/search.js',',','js')
			.Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-js','Picbox/js/picbox.js',',','js')
			
			.Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-js','jPlayer/skin/pink.flag/jplayer.pink.flag.css',',','css')
			.Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-js','jPlayer/js/jquery.jplayer.min.js',',','js')
			.Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-js','jPlayer/js/jplayer.playlist.min.js',',','js')
			;
			//Dinh nghia current modul code
			$this->view->currentModulCode = "SEARCH";
			$this->view->currentModulCodeForLeft = $this->_request->getParam('action');
	        $response->insert('header', $this->view->renderLayout('header.phtml','./application/views/scripts/'));    	//Hien thi header 
			$response->insert('menu', $this->view->renderLayout('menu.phtml','./application/views/scripts/'));    		//Hien thi header 		
	        $response->insert('footer', $this->view->renderLayout('footer.phtml','./application/views/scripts/')); 			
		}else{
			$result = array();
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-js','Picbox/css/picbox.css',',','css',$result);
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-style','mtl/search.css',',','css',$result);
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-js','mtl/search.js',',','js',$result);
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-js','Picbox/js/picbox.js',',','js',$result);
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-js','jPlayer/skin/pink.flag/jplayer.pink.flag.css',',','css',$result);
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-js','jPlayer/js/jquery.jplayer.min.js',',','js',$result);
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-js','jPlayer/js/jplayer.playlist.min.js',',','js',$result);
			$this->view->arrJsCss = Zend_Json::encode($result);				
			$baseurl=$this->_request->getbaseurl();			 
		}
	}	
	/**
	 * @author:TuanPm
	 * @see: 08/01/2013
	 * @todo: Phương thức hiển thị dữ liệu cho toàn bộ các Action
	 * Enter description here ...
	 */
	public function trainingAction(){
		$arrWards = array();		
		$objFunction = New Efy_Function_RecordFunctions();	

		$this->view->arrListItem = $objFunction->getAllObjectbyListCode('','DM_SO_HS_TREN_TRANG');
		$this->view->leftTree = $objFunction->genLeftTree();
		$this->view->titleBody = 'DANH SÁCH BÀI GIẢNG';
		$this->view->iCurrentPage = 1;
		$this->view->iNumberRecordPerPage = 15;
        //$arrTemp =  $this->_request->getParams();
	}

	public function learningAction(){
		$arrWards = array();		
		$objFunction = New Efy_Function_RecordFunctions();	
		$this->view->arrListItem = $objFunction->getAllObjectbyListCode('','DM_SO_HS_TREN_TRANG');
		$this->view->leftTree = $objFunction->genLeftTree();
		$this->view->titleBody = 'DANH SÁCH TƯ LIỆU HỌC TẬP';
		$this->view->iCurrentPage = 1;
		$this->view->iNumberRecordPerPage = 15;
	}
	public function indexdataAction(){
		//Tao doi tuong
		$objFunction = New Efy_Function_RecordFunctions();
		$objconfig = new Efy_Init_Config();	
		$objModel = new mtl_modSearch();		
		$arrResult = array();
		$typeSearch = $this->_request->getParam('type','');
		$typeTree = $this->_request->getParam('typehdn','');
		$path = $this->_request->getParam('hdnpath','');
		$link_db_user  = $objconfig->_setDbLinkUser();
		$ownerCode = Efy_Publib_Library::_getItemAttrById($_SESSION['arr_all_staff'],$_SESSION['staff_id'],'ownerCode');
		$StaffId = $_SESSION['staff_id'];
		if ($typeSearch == 'on') $typeSearch = 'All';
			else $typeSearch = '';
		$TD18 = $this->_request->getParam('TD18','');
		$TD18 = Efy_Publib_Library::_Replace_VietNamese($TD18);
		$TD18 = strtolower($TD18);
		// Lay duong dan
		$td03 = $this->_request->getParam('TD03','');
		$breadcrumbs = $objFunction->genbreadcrumbs($td03);

		//Mảng lưu tiêu chí tìm kiếm
		$arrParameter = array(
			'TD03'							=> $td03,
            'dFromDate'                     => $this->_request->getParam('hdn_from_date',''),
            'dToDate'                       => $this->_request->getParam('hdn_to_date',''),
			'TD18'              		    => $TD18,
			'type'                 			=> $typeSearch,
			'TR06'                			=> $typeTree,
			'TR09'                		 	=> $ownerCode,
			'TD21'							=> $StaffId,
            'sstaff_permisstion'            => $_SESSION['STAFF_PERMISSTION'],
			'link_db_user'					=> $link_db_user,
			'iCurrentPage'         			=> $this->_request->getParam('hdn_current_page',''),
			'iNumberRecordPerPage' 			=> $this->_request->getParam('hdn_record_number_page','')				
		);
		$arrStatus = $objFunction->getAllObjectbyListCode($_SESSION['OWNER_CODE'],'DM_TRANG_THAI');
		if($typeTree == 'TRAINING')
			$sp = 'mTL_SearchTrainingGetAll';
			else $sp = 'mTL_SearchLearningGetAll';
		$arrResult = $objModel->mTLSPRun($sp,$arrParameter, 1,0);
		// var_dump($arrResult);die();
		if ($arrResult)
			for ($i=0; $i < sizeof($arrResult); $i++) { 			
				$arrResult[$i]['TD23'] = ($arrResult[$i]['TD23'] != null)?$arrResult[$i]['TD23']:'';
				$arrResult[$i]['TD19'] = ($arrResult[$i]['TD19'] != null)?str_replace('!@!', ', ', $arrResult[$i]['TD19']):'';
				$arrResult[$i]['TD06'] = Efy_Library::_getNameByCode($arrStatus,$arrResult[$i]['TD06'],'C_NAME');
				//Lay file dinh kem
				$fileNameThumb = isset($arrResult[$i]['C_FILENAME_THUMB']) ? $arrResult[$i]['C_FILENAME_THUMB'] :'';
				// $fileNameThumb = $arrResult[$i]['C_FILENAME_THUMB'];
				if(is_null($fileNameThumb) || $fileNameThumb ==''){
					$arrResult[$i]['C_FILENAME_THUMB'] = $objconfig->_setAttachFileUrlPath().'default/default.jpg';
				}else{
					$arrFilename = explode('!~!',$fileNameThumb);
					$file_id   = explode("_", $arrFilename[0]);
		        	$srcThumBnail = $objconfig->_setAttachFileUrlPath() . $file_id[0] . "/" . $file_id[1] . "/" . $file_id[2] . "/" . $fileNameThumb;
		        	$pathFileExist = '..' . str_replace("/","\\",$srcThumBnail);
					if(file_exists($pathFileExist)) 
						$arrResult[$i]['C_FILENAME_THUMB'] = $srcThumBnail;
					else
						$arrResult[$i]['C_FILENAME_THUMB'] = $objconfig->_setAttachFileUrlPath().'default/default.jpg';
				}
			}
		echo Zend_Json::encode(array('breadcrumbs'=>$breadcrumbs,'arrResult'=>$arrResult));
		$this->getHelper('viewRenderer')->setNoRender();  		
	}
	public function getChildNode($idNode,$arrTreeGetAll){
		$arrOutput = array();
		foreach ($arrTreeGetAll as $key => $value) {
			if($value['FK_OBJECT'] == $idNode){
				array_push($arrOutput, array('attr'=>array(
												'id'=>$value['PK_OBJECT']
												,'rel'=>'folder'
											),
											'data'=>$value['TR04'],
											'state'=>'closed'
				));
			}
		}
		return $arrOutput;
	}
	public function loadtreeAction(){
		$objTree	= new Listxml_modTree();
		$arrTree = '';
		$sType = $this->_request->getParam('sType',null);
		$searchNodes = $this->_request->getParam('searchNodes',null);
		$ownerCode = Efy_Publib_Library ::_getItemAttrById($_SESSION['arr_all_staff'],$_SESSION['staff_id'],'ownerCode');
		$arrParameter = array(
			'status'=>'HOAT_DONG'
			,'txtFullTextSearch'=>''
			,'type'=>$sType
			,'sOwner_code'=>$ownerCode
		);
		if ($sType == 'TRAINING'){
			if(!isset($_SESSION['TREE_TRAINING'])){
				$objTree	= new Listxml_modTree();
				$_SESSION['TREE_TRAINING'] = $objTree->mTL_TreeGetAll($arrParameter);
			}
			$arrTreeGetAll = $_SESSION['TREE_TRAINING'];
		}
		else if ($sType == 'LEARNING'){
			if(!isset($_SESSION['TREE_LEARNING'])){
				$objTree	= new Listxml_modTree();
				$_SESSION['TREE_LEARNING'] = $objTree->mTL_TreeGetAll($arrParameter);
			}
			$arrTreeGetAll = $_SESSION['TREE_LEARNING'];
		}
		$idNode = $this->_request->getParam('id',null);
		$arrTree = $this->getChildNode($idNode,$arrTreeGetAll);
		if ($searchNodes != '')
			$arrTree = $arrTreeGetAll;
		echo json_encode($arrTree);
		$this->getHelper('viewRenderer')->setNoRender();  
	}
	public function viewtrainingAction(){
		$objFunction = New Efy_Function_RecordFunctions();
		$objLib = new Efy_Library();
		$objModel = new mtl_modSearch();
		$objconfig = new Efy_Init_Config();
		
		$this->view->titleBody = 'Thông tin bài giảng';
		$PkDoc = $this->_request->getParam('PkDoc',null);
		$typeTree = $this->_request->getParam('typehdn','');
		$arrParameter = array('PkDoc' => $PkDoc);
		$arrStatus = $objFunction->getAllObjectbyListCode($_SESSION['OWNER_CODE'],'DM_TRANG_THAI');
		if($typeTree == 'TRAINING')
			$sp = 'mTL_SearchTrainingGetSingle';
			else $sp = 'mTL_SearchLearningGetSingle';
		$arrResult = $objModel->mTLSPRun($sp,$arrParameter, 0,0);
		$arrResult['TD23'] = ($arrResult['TD23'] != null)?$arrResult['TD23']:'';
		$arrResult['TD19'] = ($arrResult['TD19'] != null)?str_replace('!@!', ', ', $arrResult['TD19']):'';
		$arrResult['TD06'] = Efy_Library::_getNameByCode($arrStatus,$arrResult['TD06'],'C_NAME');
		$sFile = '';
		//Lay file dinh kem
		$strFileName 	= $arrResult['TD23'];
		$viewvideo = $viewdocument = $viewimage ='';
		$videos = array();
		if($strFileName){
			$arrParseFile = $objLib->parseAttachFile($strFileName,"!~~!","!~!",$objconfig->_setAttachFileUrlPath());
			// var_dump($arrParseFile);
			$images = $arrParseFile['images'];
			$videos = $arrParseFile['videos'];
			$documents = $arrParseFile['documents'];
			if($images){
				$viewimage = $objLib->genfilmstripimage($images);
			}
			if($documents){
				$viewdocument = $objLib->genfilmstripdocument($documents);
			}
			if($videos){
				$viewvideo = $objLib->genfilmstripvideo($videos);
			}
		}

		$jsonvideo = Zend_Json::encode($videos);
		// echo $viewimage;
		$this->view->viewimage = $viewimage;
		$this->view->viewdocument = $viewdocument;
		$this->view->viewvideo = $viewvideo;
		$this->view->jsonvideo = $jsonvideo;
		$this->view->arrResult = $arrResult;
	}
	public function loadcommentAction(){
		$objFunction = New Efy_Function_RecordFunctions();
		$objModel = new mtl_modSearch();
		$PkDoc = $this->_request->getParam('PkDoc',null);
		$PkComment = $this->_request->getParam('PkComment',null);
		$staffId = $_SESSION['staff_id'];
		$arrParameter = array('RC01'=>$PkComment
								,'RC02' => $PkDoc
								,'staffId' 		=> $staffId);
		$arrCommentGetAll = $objModel->mTLSPRun('mTL_CommentGetAll',$arrParameter, 1,0);
		echo Zend_Json::encode($arrCommentGetAll);
		$this->getHelper('viewRenderer')->setNoRender(); 
	}
	public function updatecmmtAction(){
		$objFunction = New Efy_Function_RecordFunctions();
		$objModel = new mtl_modSearch();
		$PkDoc = $this->_request->getParam('PkDoc',null);
		$RC01 = $this->_request->getParam('PkComment',null);
		$cmmt = $this->_request->getParam('cmmt',null);
		$sStaffName = Efy_Publib_Library ::_getItemAttrById($_SESSION['arr_all_staff'],$_SESSION['staff_id'],'position_code'). ' - '.Efy_Publib_Library ::_getItemAttrById($_SESSION['arr_all_staff'],$_SESSION['staff_id'],'name');
		$sStaffId = Efy_Publib_Library ::_getItemAttrById($_SESSION['arr_all_staff'],$_SESSION['staff_id'],'id');
		$arrParameter = array(
			'RC01'			=> $RC01,
			'RC02'			=> $PkDoc,
			'RC03'			=> $sStaffId,
			'RC04'			=> $sStaffName,
			'RC06'			=> $cmmt,
			);
		$arrCommentUpdate = $objModel->mTLSPRun('mTL_CommentUpdate',$arrParameter, 0,0);
		var_dump($arrCommentUpdate);
		$this->getHelper('viewRenderer')->setNoRender(); 
	}
	public function deletecmmtAction(){
		$objFunction = New Efy_Function_RecordFunctions();
		$objModel = new mtl_modSearch();
		$arrParameter = array('PkComments' 			=> $this->_request->getParam('PkComments',null)
							,'delimitorElement'		=> $this->_request->getParam('delimitorElement',null));  
		$arrResult = $objModel->mTLSPRun('mTL_CommentDelete',$arrParameter, 0,0);
		var_dump($arrResult);
		$this->getHelper('viewRenderer')->setNoRender();
	}
}
?>
